package com.hspedu.newData.dmsxl.数组;

import java.util.TreeSet;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-13 
 * @Version: 1.0
 */

public class LC414第三大的数 {

    public int thirdMax(int[] nums) {

        TreeSet<Integer> set = new TreeSet<>();

        for (int i = 0; i < nums.length; i++) {

            set.add(nums[i]);

            if (set.size()>3){
                set.remove(set.first());
            }
        }

        return set.size() == 3 ? set.first() : set.last();
    }
}
